package a66weding.com.jiehuntong.view;

import android.content.Context;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2016/7/24.
 */
public class CustomViewPager2 extends ViewPager {
    public int index;
    private Map<Integer, Integer> maps = new HashMap<Integer, Integer>();
    private int current;

    private int currentMax;

    public CustomViewPager2(Context context) {
        super(context);
    }

    public CustomViewPager2(Context context, AttributeSet attrs) {
        super(context, attrs);
    }


    public void notifyView(int position) {

        index=position;
        requestLayout();


    }


    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int height = 0;

        for (int i = 0; i < getChildCount(); i++) {
            View child = getChildAt(i);
            child.measure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
            int h = child.getMeasuredHeight();

            if (h > height)
               height = h;
        }

        if(height>0) {
            currentMax=height;
        }
        heightMeasureSpec = MeasureSpec.makeMeasureSpec(Math.max(currentMax,height), MeasureSpec.EXACTLY);


        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }



    private int measureHeight(int measureSpec, View view) {
        int result = 0;
        int specMode = MeasureSpec.getMode(measureSpec);
        int specSize = MeasureSpec.getSize(measureSpec);

        if (specMode == MeasureSpec.EXACTLY) {
            result = specSize;
        } else {
            if (view != null) {
                result = view.getMeasuredHeight();
            }
            if (specMode == MeasureSpec.AT_MOST) {
                result = Math.min(result, specSize);
            }
        }
        return result;
    }


    public int measureFragment(View view) {
        if (view == null)
            return 0;

        view.measure(0, 0);
        return view.getMeasuredHeight();
    }


}
